$(function(){
	var host = "https://cloud.kaaction.com/crab/api/v1/";
	var str = '';
	/*生成4位随机数*/
	function rand(){
		$.ajax({
			url : host + "crabValidateCodeApi/getValidateCode",
			async: false,
			dataType:'json',
			success:function (res) {
				str= res.data;
			}
		})
		var arr=str.split("");
		var validate="";
		for(var i=0;i<4;i++){
		   validate+=arr[i];
	}
	return validate;
	}
	 /*干扰线的随机x坐标值*/
	function lineX(){
	  var ranLineX=Math.floor(Math.random()*90);
	  return ranLineX;
	}
	 /*干扰线的随机y坐标值*/
	function lineY(){
	  var ranLineY=Math.floor(Math.random()*40);
	  return ranLineY;
	 }
	function clickChange(){
	    var mycanvas=document.getElementById('mycanvas');
	    var cxt=mycanvas.getContext('2d');
	    cxt.fillStyle='#f8f8f8';
	    cxt.fillRect(0,0,100,40);
	    /*生成干扰线20条*/
		for(var j=0;j<20;j++){
		   cxt.strokeStyle='#d9d9d9';
		   cxt.beginPath(); //若省略beginPath，则每点击一次验证码会累积干扰线的条数
		   cxt.moveTo(lineX(),lineY());
		   cxt.lineTo(lineX(),lineY());
		   cxt.lineWidth=0.5;
		   cxt.closePath();
		   cxt.stroke();
		}
	    cxt.fillStyle='#094f02';
	    cxt.textAlign='center';
	    cxt.font='400 20px Arial';
	    cxt.textBaseline='middle';
	    cxt.fillText(rand(),50,20); //把rand()生成的随机数文本填充到canvas中  
	 }
	 /*点击验证码更换*/
	clickChange();
	mycanvas.onclick=function(e){
	   e.preventDefault(); //阻止鼠标点击发生默认的行为
	   clickChange();
	};
	// 卡号验证处理
	var statusnum = '';
	$('.card-buttom img').click(function(){
		sessionStorage.clear();
		var card = $('#cardnum').val();
		var password = $('#password').val();
		var code = $('#code').val();
		if(card && password && code){
			$.ajax({
				url:host + 'crabLoginApi/loginCheck',
				dataType:'json',
				data:{
					cardNum:card,
					password:password,
					vcode:code
				},
				success:function(res){
					console.log(res)
					if(res.err_code == 'fail'){
						$('span.warning').html(res.err_msg);
						clickChange();
					}else{                                                                    
						sessionStorage["cardnumber"] = card;
						sessionStorage["cardpassword"] = password;
						window.location.href = 'result.html';
					}					
				}
			})
		}else{ 
			$('span.warning').html('请将信息填写完整！');
			clickChange();
		}	
	})
	$('input').focus(function(){
		$('span.warning').html('');
	})
})

















